//
//  VisitorsView.swift
//  Weibo
//
//  Created by rzj on 16/3/10.
//  Copyright © 2016年 rzj. All rights reserved.
//

import UIKit
import SnapKit

/** 代理方法 */
@objc protocol VisitorsViewDelegate: NSObjectProtocol {

    /* 登录按钮点击方法 */ //可选协议必须是@objc开头协议
   optional func visitorsView(visitorsView:VisitorsView,
        btnClickLogin:UIButton)

}

class VisitorsView: UIView {

    //初始化代理 使用weak 否则循环引用 在swift中使用weak 协议必须基于NSObjectProtocol
    weak var delegate:VisitorsViewDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /* 供外界设置访客视图 **/
    func SetUpPageInfo(imageName: String?, message: String?) {
    
        //为nil 默认为首页
        if imageName == nil {
            circleView.hidden = false
            startAnimation() 
        }else {
            //不是首页隐藏图标 设置图片
            circleView.hidden = true
            iconView.image = UIImage(named: imageName!)
            messageLabel.text = message
        }
    
    }
    
    
    //3,开启转圈动画
    private func startAnimation() {
        //初始化动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //参数值（旋转360度）
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        //执行一次动画的时长
        animation.duration = 20
        //切换界面不删除动画
        animation.removedOnCompletion = false
        //添加动画
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    //MARK: - 懒加载
    
    //大图标
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //圆圈
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //消息label(使用遍历构造函数设置)
    private lazy var messageLabel:UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSzie: 14)
        label.text = "按时打算打算打打打打打死阿萨蓝色的男篮是到哪都懒得"
        label.textAlignment = .Center
        label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    private lazy var registerBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: "btnOnlickLogin:", forControlEvents: .TouchUpInside)
        return btn
    }()
    
    //登录按钮
    private lazy var loginBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: "btnOnlickLogin:", forControlEvents: .TouchUpInside)
        return btn
    }()
    
    //设置代理
    @objc private func btnOnlickLogin(btn:UIButton) {
        
        //visitorsView? 加问号判断能否响应协议可选方法
        delegate?.visitorsView?(self, btnClickLogin: btn)
    
    }
    
    
    //圆圈遮罩
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

}


extension VisitorsView {
    
    //设置视图
    private func setUpUI() {
        
        //设置背景色与圆圈遮罩view颜色一样 设置一种RGB相同颜色使用-white
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        //1，添加子控件 先添加circleView 因为他要遮住circleView 不能遮住iconView 放后面
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        
        //2，添加约束 保证控件在父控件上才能添加约束!!!
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        
        //圆圈约束
        circleView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.centerY.equalTo(iconView.snp_centerY)
        }
        
        //消息label约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom)
            make.width.equalTo(224)
        }
        
        //注册按钮约束
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        //登录按钮约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(registerBtn.snp_top)
            make.width.equalTo(100)
        }
        
        //圆圈遮罩view
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
        
    }
    
//    //设置视图
//    private func setUpUI() {
//    
//        //设置背景色与圆圈遮罩view颜色一样 设置一种RGB相同颜色使用-white
//        backgroundColor = UIColor(white: 237/255, alpha: 1)
//        
//        //1，添加子控件 先添加circleView 因为他要遮住circleView 不能遮住iconView 放后面
//        addSubview(circleView)
//        addSubview(maskIconView)
//        addSubview(iconView)
//        addSubview(messageLabel)
//        addSubview(registerBtn)
//        addSubview(loginBtn)
//        
//        
//        //2，添加约束 保证控件在父控件上才能添加约束!!!
//        
//        //注意使用自动布局AutoLayout关闭Autoresizing
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        //控件的中心x，y与view中心一致
//        /*
//        item  要约束的控件
//        attribute  约束的属性
//        relatedBy  约束关系（大于，小于，等于）
//        toItem  相对约束控件
//        attribute 约束的属性
//        multiplier 乘以系数
//        constant   加上常量(偏移量)
//        */
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//        
//        //圆圈约束
//        circleView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
//        
//        //消息label约束
//        messageLabel.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top , relatedBy: .Equal, toItem: circleView, attribute: .Bottom , multiplier: 1, constant: 16))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
//        
//        //注册按钮约束
//        registerBtn.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
//        
//        //登录按钮约束
//        loginBtn.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
//        loginBtn.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: registerBtn, attribute: .Top, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
//        
//        //圆圈遮罩view
//        maskIconView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//        
//    }
    
}

